<template>
	<view class="container">
		<u--image :showLoading="true" src="/static/welcome.png" width="100%" height="auto" mode="widthFix"></u--image>
	</view>
</template>

<script>
	import {
		getOpenIdApi,
	} from '@/apis/common.js';
	export default {
		data() {
			return {
				code: '',
				token: ''
			}
		},
		onLoad(options) {
			this.code = this.getQueryString(window.location.search, "code") || '';
			let token = this.getQueryString(window.location.search, "token") || '';
			if (token) {
				uni.setStorageSync('token', resp.token);
				this.$commonUtil.redirectTo('/pages/index/index', {}, 'reLaunch')
			} else {
				if (this.code) {
					this.getOpenid();
				} else {
					this.$commonUtil.redirectTo('/pages/login/login')
				}
			}

		},
		methods: {
			/*获取当前页面链接中的参数*/
			getQueryString(str, key) {
				if (str) {
					let queryString = str.split('?')[1] || '';
					let arr = queryString.split('&') || [];
					for (let i = 0; i < arr.length; i++) {
						let keyString = decodeURIComponent(arr[i].split('=')[0]);
						let valueString = decodeURIComponent(arr[i].split('=')[1]);
						if (key === keyString) {
							return valueString;
						}
					}
					return;
				} else {
					return;
				}
			},
			async getOpenid() {
				try {
					let resp = await getOpenIdApi({
						code: this.code
					})
					if (resp.isBind) {
						uni.setStorageSync('token', resp.token);
						this.$commonUtil.redirectTo('/pages/index/index', {}, 'reLaunch')
					} else {
						this.$commonUtil.redirectTo('/pages/login/login', {
							openid: resp.openid
						}, 'reLaunch')
					}
				} catch (e) {
					console.log(`获取openid${e.message}`, 'background:orange');
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	.container {
		width: 100%;
		min-height: 100%;
	}
</style>